package com.biqee.service.bq.impl;

import java.util.List;

import com.biqee.dao.BqDictDAO;
import com.biqee.framework.spring.OnConextFinishedListener;
import com.biqee.model.db.BqDict;
import com.biqee.service.bq.BqDictService;
import com.biqee.util.SegmentUtil;
import com.biqee.util.StringUtil;

public class BqDictServiceImpl implements BqDictService ,OnConextFinishedListener{
	
	private BqDictDAO bqDictDAO ; 
	
	
	






	public void setBqDictDAO(BqDictDAO bqDictDAO) {
		this.bqDictDAO = bqDictDAO;
	}




	@Override
	public void addWords(String words) {
		if(StringUtil.isBlank(words)){
			return ;
		}
		
		String[] wordsArray = words.split("[ ,;]");
		for(String word: wordsArray){
			if(StringUtil.isNotBlank(word)){
				createIfNotExist(word);
			}
		}
		
	}




	private void createIfNotExist(String word) {
		word = StringUtil.trim(word);
		BqDict dict = bqDictDAO.getByWord(word);
		if(dict==null){
			dict = new BqDict();
			dict.setWord(word);
			bqDictDAO.insert(dict);
			SegmentUtil.addDicWords(word);
		}
	}




	@Override
	public List<String> listAllWords() {
		return bqDictDAO.listAllWords();
	}




	



	@Override
	public void initMyDict() {
		List<String> allwords = listAllWords();
		if(allwords!=null&&!allwords.isEmpty()){
			SegmentUtil.addDicWords(allwords);
		}
	}




	@Override
	public void execute() {
		initMyDict();
		
	}
	
	
}
